package com.bosssoft.trainee.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.trainee.common.annotation.Description;
import com.bosssoft.trainee.mbg.dto.RoleDTO;
import com.bosssoft.trainee.mbg.po.Role;
import com.bosssoft.trainee.mbg.vo.RoleVO;
import com.bosssoft.trainee.mbg.result.CommonResult;
import com.bosssoft.trainee.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * @Author: wuxin
 * @Date: 2022/6/17
 * @Project rbac
 * @Version 1.0
 * @Description
 */
@RestController
@RequestMapping(value = "/role")
@Validated
public class RoleController {
    @Autowired
    RoleService roleService;

    @Description("非角色页面获取角色列表")
    @GetMapping(value = "/getRoles")
    public CommonResult<Page<Role>> getRoles(@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {
        return CommonResult.success(roleService.getRoles(pageSize, pageNum));
    }

    @Description("获取角色列表")
    @GetMapping(value = "/list")
    public CommonResult<Page<Role>> getList(@RequestParam(value = "example", required = false) RoleVO roleVO,
                                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {
        RoleDTO example = new RoleDTO();
        BeanUtil.copyProperties(roleVO, example);
        return CommonResult.success(roleService.getList(pageSize, pageNum, example));
    }

    @Description("新增角色")
    @PostMapping(value = "/addRole")
    public CommonResult<String> addRole(@Validated @RequestBody RoleVO roleVO) {
        RoleDTO roleDTO = new RoleDTO();
        BeanUtil.copyProperties(roleVO, roleDTO);
        return roleService.addRole(roleDTO) > 0 ? CommonResult.success("添加成功") : CommonResult.failed("添加失败");
    }

    @Description("修改角色")
    @PostMapping(value = "/updateRole")
    public CommonResult<String> updateRole(@Validated @RequestBody RoleVO roleVO) {
        if (roleVO.getId() == null) {
            return CommonResult.failed("修改失败,id不能为空");
        }
        RoleDTO roleDTO = new RoleDTO();
        BeanUtil.copyProperties(roleVO, roleDTO);
        return roleService.updateRole(roleDTO) > 0 ? CommonResult.success("修改成功") : CommonResult.failed("修改失败");
    }

    @Description("根据id删除角色")
    @GetMapping(value = "/deleteById")
    public CommonResult<String> deleteRoleById(@RequestParam("id") int id) {
        return roleService.deleteById(id) > 0 ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }

    @Description("检查角色代码是否被占用")
    @GetMapping(value = "/checkCode")
    public CommonResult<Boolean> checkCode(@NotBlank(message = "角色代码不能为空")
                                           @Pattern(regexp = "^\\d{4}$", message = "角色代码格式不正确")
                                           @RequestParam("code") String code) {
        return CommonResult.success(roleService.checkCode(code));
    }
}
